const {
	$_ULOG,
} = require('../logger/logger')
const {
	getTime,
} = require('../../utils/dateUtils')
const {
	encPwd,
	funcVerify,
} = require('../utils')
const Response = require('./Response')
const funcDAO = require('../../utils/dao/func-dao')

module.exports = {

	Response,

	list: async function(req) {
		if (!funcVerify(req, 'func_list'))
			return new Response('201', '没有权限')
		let {
			role_ids,
		} = req.session._userInfo
		return funcDAO.list(req.body, role_ids)
	},

	add: async function(req) {
		if (!funcVerify(req, 'func_add'))
			return new Response('201', '没有权限')
		let func = await funcDAO.add(req.body)
		if (func == -1)
			return new Response('202', '模块ID已存在！')
		if (func == -2)
			return new Response('202', '权限ID已存在！')
		return new Response('000', '成功', func)
	},

	modify: async function(req) {
		if (!funcVerify(req, 'func_modify'))
			return new Response('201', '没有权限')
		let func = await funcDAO.modify(req.body)
		if (!func || !func._id)
			return new Response('203', '修改失败')
		return new Response('000', '成功', func)
	},

	del: async function(req) {
		if (!funcVerify(req, 'func_del'))
			return new Response('201', '没有权限')
		let func = await funcDAO.del(req.body)
		if (func == -1)
			return new Response('203', '存在权限，不能删除！')
		if (func == -2)
			return new Response('203', '有子模块，不能删除！')
		if (func == -3)
			return new Response('203', '基础权限，不能删除！')
		return new Response('000', '成功')
	},

	funcModule: async function(req) {
		if (!funcVerify(req, 'func_add'))
			return new Response('201', '没有权限')
		let result = await funcDAO.funcModule(req.body)
		return new Response('000', '成功', result)
	},

}